<?php
/*
 * 
 *   子系统管理
 *   
 *   */
namespace Dyadmin\Controller;

use Think\Controller;

class UserController extends CommomController
{

    public function index()
    {
        $this->check('3_0');

        //发送数据
        if (IS_POST) {
            $map['login_name'] = I("post.login_name");
            $map['config_name'] = I("post.config_name");
            $map['timestar'] = I("post.timestar");
            $map['timeend'] = I("post.timeend");
            $map['is_agent'] = I("post.is_agent");
            $_GET['p'] = 1;

        }else{
            
            $map['login_name'] = I("get.login_name");
            $map['config_name'] = I("get.config_name");
            $map['timestar'] = I("get.timestar");
            $map['timeend'] = I("get.timeend");
            $map['is_agent'] = I("get.is_agent");
        }
        if (!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['login_time'] = array('between', array($stime1, $etime1));
        }
        if (!empty($map['login_name'])) {
            $where['login_name'] = $map['login_name'];
        }
        if (!empty($map['config_name'])) {
            $where['config_name'] = $map['config_name'];
        }
          if ( $map['is_agent']!= '') {
            $where['is_agent'] = $map['is_agent'];
        }
        //查询出最后一条数据
        $count =  M('WebConfig  as  s')->where($where)->join('qz_user  as  t  on s.config_id = t.config_id')->count();
        //设置一页多少条数据
        $page = new \Think\Page ($count, 10, $map);
       
        //显示分页
        $show = $page->show();
        //根据提交过来的查询条件。查询出用户信息
        $data = M('WebConfig  as  s')->join('qz_user  as  t  on s.config_id = t.config_id')
            ->field('s.config_id,s.config_name,t.login_name,t.user_id,t.username,t.money,t.level_name,t.reg_time,t.is_agent')
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign($map);
        $this->assign("page", $show);
        $this->assign("data", $data);
        $this->display();
    }
    
    public function  is_agent(){
        
        $id = I('get.id');
        $data = M('User')->where(['user_id'=>$id])->field('is_agent')->find();
        if(empty($data) ){
           $this->error("会员未找到",U('User/index'));
         }
        $is_agent=$data['is_agent'];
        if($is_agent=='1'){
            $is_agent='0';
        }else{
            $is_agent='1';
        }
        
       $re =M('User')->where(['user_id'=>$id])->save(['is_agent'=>$is_agent]);
       if($re){
             $this->error("修改成功",U('User/index'));
       }else{
             $this->error("修改失败",U('User/index'));
       }
        
    }

    //会员详情
    public function details()
    {
        $id = I('get.id');
        $data = M('User')->find($id);
        $level_name1=M('User')->where('user_id='.$data['level1_id'])->find();
        
        
      
        //获取身份信息
       	$mer_data= M('mer_apply')->where(['user_id'=>$id])->find();
       	//照片审核信息
       	$img_order=M('MerImgOrder')->where(['user_id'=>$id])->find();
    	if(empty($img_order)){
    	 $img_con = '3';
    	}else {
    		
    		$img_con=$img_order['status'];
    	}
    	
         //获取下级会员列表
        $where['level1_id'] = $id;
        $count=M('User')->where($where)->count();
        
        $level_data=M('user_level')->where(['config_id'=>$data['config_id'],'level'=>$data['user_level']])->find();
       
        $page = new \Think\Page ($count, 4);
        //显示分页
        $show = $page->show();
        
        
        $sub_list=M('User')->field('login_name,user_id,reg_time,level_name,username')
                           ->where($where)
                           ->limit($page->firstRow . ',' . $page->listRows)
                           ->select();
       
        $status_arr=array('0'=>'正常','1'=>'异常','2'=>'未认证','3'=>'已认证');
        $img_confirm=array('0'=>'未审核','1'=>'审核通过','2'=>'审核未通过','3'=>'未提交资料');
        $this->assign("data", $data);
        $this->assign('status_arr',$status_arr);
        $this->assign("level_name1", $level_name1);
        $this->assign("level_name2", $level_name2);
        $this->assign("level_name3", $level_name3);
        $this->assign("level_data", $level_data);
        $this->assign("mer", $mer_data);
        $this->assign("img_confirm", $img_confirm);
        $this->assign("img_con", $img_con);
        $this->assign("sub_list", $sub_list);
        $this->assign("page", $show);
        $this->display();
    }

    /**
     *会员等级
     */
    public function level()
    {

        $this->check('3_1');
        $data = M("UserLevel")->select();
        $this->assign("data", $data);
        $this->display();
    }

    //会员等级添加
    public function level_add()
    {
        $this->check('3_1');
        $user_level = M('UserLevel');
        if (IS_POST) {
            $data['level'] = I('post.level');
            $data['name'] = I('post.name');
            $data['money'] = I('post.money');
            $data['intr_num'] = I('post.intr_num');
            $data['intr_cost'] = I('post.intr_cost');
            $data['rate'] = I('post.rate') ;
            if ($user_level->create($data)) {
                //执行修改
                if ($user_level->add()) {

                    $this->success('会员等级添加成功！','/Dyadmin/User/level');
                } else {
                    $this->error('会员等级添加失败！');
                }
            } else {
                $this->error('会员等级添加失败！');
            }
            return;
        }
        $this->display();
    }


    //会员等级修改页面
    public function level_edit()
    {
        $this->check('3_1');
        $id = I('get.id');
        $data = M('UserLevel')->find($id);
        $this->assign("data", $data);
        $this->display();
    }
    //执行会员等级修改
    public function level_update()
    {
        $this->check('3_1');
        $user_level = M('UserLevel');
        if (IS_POST) {
            $data['level_id'] = I('post.level_id');
            $data['level'] = I('post.level');
            $data['name'] = I('post.name');
            $data['money'] = I('post.money');
            $data['intr_num'] = I('post.intr_num');
            $data['intr_cost'] = I('post.intr_cost');
            $data['rate'] = I('post.rate');
            if ($user_level->create($data)) {
                //执行修改
                if ($user_level->save()) {

                    $this->success('会员等级信息修改成功！','/Dyadmin/User/level');
                } else {
                    $this->error('会员等级信息修改失败！');
                }
            } else {
                $this->error('会员等级信息修改失败！');
            }
            return;
        }
        $this->display();
    }

    //会员等级信息删除
    public function level_del()
    {
        $this->check('3_1');
        if (IS_POST) {
            $where['level_id'] = I("post.id");
            if (empty($where['level_id'])) {
                $resul['info'] = "信息错误";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $role = M("UserLevel")->where($where)->find();
            if ($role) {
                $re = M("UserLevel")->where($where)->delete();
                if ($re) {
                    $re = D("LogAdmin")->insert_log("删除角色" . $role['name']);
                    $resul['info'] = "删除成功";
                    $resul["status"] = 'y';
                    $this->ajaxReturn($resul, 'JSON');
                } else {
                    $resul['info'] = "系统错误";
                    $resul["status"] = 'n';
                    $this->ajaxReturn($resul, 'JSON');
                }
            } else {
                $resul['info'] = "已删除";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $this->ajaxReturn($_POST, 'JSON');
        }
        $this->display();
    }


    //会员返利页面
    public function user_rebate()
    {	 $this->check('3_1');
        if (IS_POST) {
            $map['order_id'] = I("post.order_id");
            $map['username'] = I("post.username");
            $map['timestar'] = I("post.timestar");
            $map['timeend'] = I("post.timeend");
            $_GET['p'] = 1;

        }else {
        	   $map['type'] = I("get.type");
        	   $map['user_id'] = I("get.user_id");
        	
        }
        if (!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['created_at'] = array('between', array($stime1, $etime1));
        }
        if (!empty($map['order_id'])) {
            $where['order_id'] = $map['order_id'];
        }
        if (!empty($map['username'])) {
            $where['username'] = $map['username'];
        }
        
        if (!empty($map['type'])) {
        	$where['type'] = $map['type'];
        }
        if (!empty($map['user_id'])) {
        	$where['user_id'] = $map['user_id'];
        }
       
        //查询出最后一条数据
        $count =  M('UserRebateRecord')->where($where)->count();
        //设置一页多少条数据
        $page = new \Think\Page ($count,10, $map);
        //显示分页
        $show = $page->show();
        $user_rebate=M('UserRebateRecord')->where($where)->order('created_at desc')->limit($page->firstRow . ',' . $page->listRows)->select();
         $arr = array('1'=>'消费返佣','2'=>'下级消费返佣','3'=>'省级代理返佣','4'=>'市级代理返佣',);
        $this->assign('arr',$arr);
        $this->assign('page',$show);
        $this->assign('data',$user_rebate);
        $this->display();
    }
    /*
     * 会员升级
     */
    public function user_data()
    {	 $this->check('3_1');
        if(IS_POST){
            $map['username'] = I ( "post.username" );
            $map['timestar']= I ( "post.timestar" );
            $map['timeend'] = I ( "post.timeend" );
            $map['mer_order_no'] = I ( "post.mer_order_no" );
            $_GET['p']=1;
            

        }
        if(!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['add_time'] = array('between',array($stime1,$etime1));
        }
        if(!empty($map['username'])){
            $where['username'] =$map['username'];
        }
        if(!empty($map['mer_order_no'])){
            $where['mer_order_no'] =$map['mer_order_no'];
        }

		$where['settle_status']=1;
        $count=M('UserUpgrade')->where($where)->count();
        $page = new \Think\Page ( $count,8,$map);
        $show = $page->show ();
        $order=M('UserUpgrade')->where($where )->order('add_time DESC ')->limit($page->firstRow . ',' . $page->listRows )->select();


        for ($i = 0; $i < count($order); $i++) {
            if ($order[$i]['status'] == 0) {
                $order[$i]['status'] = '未处理';
            } else if ($order[$i]['status'] == 1) {
                $order[$i]['status'] = '成功';
            }else if( $order[$i]['status'] !== 1 && $order[$i]['status'] !== 0){
                $order[$i]['status'] = '失败';
            }
        }
  
        $this->assign('page',$show);
        $this->assign('data',$order);
        $this->display();
    }
    
    /*
     * 升级充值返利
     */
     public function upgrade_list()
    {	 $this->check('3_1');
        if(IS_POST){
            $map['username'] = I ( "post.username" );
            $map['timestar']= I ( "post.timestar" );
            $map['timeend'] = I ( "post.timeend" );
            $map['mer_order_no'] = I ( "post.mer_order_no" );
            $_GET['p']=1;            
        }else{
        	 $map['user_id'] = I ( "get.user_id" );
        	
        }
        if(!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['add_time'] = array('between',array($stime1,$etime1));
        }
        if(!empty($map['username'])){
            $where['username'] =$map['username'];
        }
        if(!empty($map['mer_order_no'])){
            $where['mer_order_no'] =$map['mer_order_no'];
        }
        if(!empty($map['user_id'])){
        	$where['user_id'] =$map['user_id'];
        }
       
        $where['status']=1;

        $count=M('UserUpgrade')->where($where)->count();
        $page = new \Think\Page ( $count,8,$map);
        $show = $page->show ();
        $order=M('UserUpgrade')->where($where)->limit($page->firstRow . ',' . $page->listRows )->select();
        $upgrade_type =['1'=>'会员付费升级','2'=>'会员推荐了多个会员达到限定的交易金额'];

        for ($i = 0; $i < count($order); $i++) {
            if ($order[$i]['status'] == 0) {
                $order[$i]['status'] = '未处理';
            } else if ($order[$i]['status'] == 1) {
                $order[$i]['status'] = '成功';
            }else if( $order[$i]['status'] !== 1 && $order[$i]['status'] !== 0){
                $order[$i]['status'] = '失败';
            }
        }
  
        $this->assign('page',$show);
        $this->assign('data',$order);
        $this->assign('upgrade_type',$upgrade_type);
        $this->display();
    }
    
    /*
     * 升级详情
     */
    public function upgrade_detail()
    {
    	$this->check('3_1');
        $gid = I ( "get.id" ,0,'intval');
        if($gid==0){
            $this->error('该升级单不存在！');
        }
        //查找会员升级信息
        $info = M('UserUpgrade')->where(array('id'=>$gid))->find();
        if(!$info){
            $this->error('该升级单不存在！');
        }
        
        //会员升级其他信息
        $config_name=M('WebConfig')->where('config_id="'.$info['config_id'].'"')->getField('config_name');//子系统名称
        $level_before=M('UserLevel')->where(array('gid'=>$info['config_id'],'level'=>$info['level_before']))->getField('name');//升级前等级名称
        $level_after=M('UserLevel')->where(array('gid'=>$info['config_id'],'level'=>$info['level_after']))->getField('name');//升级后等级名称
        $info['order_no']=M('order')->where(array('order_id'=>$info['order_id']))->getField('mer_order_no');//订单号

        //会员升级详情列表
        $count=M('UserUpgradeDetail')->count();
        $page = new \Think\Page ( $count,8,$map);
        $show = $page->show ();
        $where['a.gid']=$gid;
        $order = M('UserUpgradeDetail')->alias('a')->field('a.*,b.login_name AS username')->join('__USER__ b ON b.user_id= a.user_id','left')->where($where)->select();
        $count = count($order);
        for ($i = 0; $i < $count; $i++) {
            if ($order[$i]['status'] == 0) {
                $order[$i]['status'] = '未处理';
            } else if ($order[$i]['status'] == 1) {
                $order[$i]['status'] = '成功';
            }else if( $order[$i]['status'] !== 1 && $order[$i]['status'] !== 0){
                $order[$i]['status'] = '失败';
            }
        }
        
        //常量数组显示
        $status_arr=array('0'=>'申请中','1'=>'成功','2'=>'失败');
        $status_settle=array('0'=>'未结算','1'=>'已结算');
        
        //变量输出
        $this->assign('page',$show);
        $this->assign('data',$order);
        $this->assign('info',$info);
        $this->assign('config_name',$config_name);
        $this->assign('level_before',$level_before);
        $this->assign('level_after',$level_after);
        $this->assign('status_arr',$status_arr);
        $this->assign('status_settle',$status_settle);
        $this->display();
    }
    
    /*
     * 充值通道
     */
    public function pass() {

    	$this->check('3_1');
        //会员升级详情列表
        $count=M('PaymentApi')->count();
        $page = new \Think\Page ($count,10);
        $show = $page->show ();
        
        $pass=M('PaymentApi')->field('id,pay_id,pay_name,fee,accounting_date,status,channel,channel_type,max_money,min_money,start_time,end_time')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
           
         //常量数组显示
         $cha_type=['1'=>'民生扫码','2'=>'快捷支付A','3'=>'百付扫码','4'=>'快捷支付B'];
        $cha=['unionpay'=>'银联支付','weixin'=>'微信','alipay'=>'支付宝','qqpay'=>'qq钱包','kjpay'=>'快捷支付'];
   
        //变量输出
        $this->assign('page',$show);
        $this->assign('pass',$pass);
        $this->assign('cha',$cha);
        $this->assign('cha_type',$cha_type);
        $this->display();
    }
    
    
    /*
     *充值通道编辑
     */
    
    public function pass_edit() {
       $id=I('get.id');
       if(empty($id)){
           
           $this->error("系统错误");
       }
        $api=M('PaymentApi')->where(['id'=>$id])->field('id,pay_id,pay_name,fee,accounting_date,status,channel,channel_type,max_money,min_money,start_time,end_time')->find();
        
        //常量数组显示
         $cha_type=['1'=>'扫码','2'=>'快捷支付A','3'=>'百付扫码','4'=>'快捷支付B'];
        $cha=['unionpay'=>'银联支付','weixin'=>'微信','alipay'=>'支付宝','qqpay'=>'qq钱包','kjpay'=>'快捷支付'];

        $this->assign('api',$api);
        $this->assign('cha',$cha);
        $this->assign('cha_type',$cha_type);
        $this->display();
    }
    
    /*
     *保存表单数据
     */
    
    public function pass_save() {
       if (IS_POST){
           
//                $cha_type=['济南扫码'=>'1','快捷支付'=>'2','百付扫码'=>'3'];
//                $cha=['银联支付'=>'unionpay','微信'=>'weixin','支付宝'=>'alipay','qq钱包'=>'qqpay','快捷支付'=>'kjpay'];

//    		$data['channel'] = trim($cha[$data['channel']]);
//    		$data['channel_type'] = trim($cha_type[$data['channel_type']]);
    		$data = I('post.');
                $action = trim($data['action']);
                $id = intval($data['good_id']);
    		$data['max_money'] = trim($data['max_money']);
    		$data['min_money'] = trim($data['min_money']);
    		$data['start_time'] = trim($data['start_time']);
    		$data['end_time'] = trim($data['end_time']);
                
    		try {
    			if('add' == $action) {
    				$ret = M('PaymentApi')->insert($data);
    			} else {
    				$data['id'] = trim($data['id']);
    				$ret = M('PaymentApi')->save($data);
    			}
    			if($ret >= 0) {
    				$result['status'] = 'y';
    				$result['info'] = '请求提交成功！';
    			} else {
    				$result['status'] = 'n';
    				$result['info'] = "数据提交处理失败！";
    			}
    		} catch(Exception $e) {
    			$result['status'] = 'n';
    			$result['info'] = $e->getMessage();
    		}
    	}else{
    		$result['status'] = 'n';
    		$result['info'] = '非法提交请求！';
    	}
    	$this->ajaxReturn($result, 'JSON');
    }
    
    /*
     * 更改开启状态
     */
    public function state_change() {
    	$this->check('3_1');
        $data = I('get.');
    	try {
    		//$ret = D('PaymentApi')->update($data['status'], $data['commend']);
    		$ret = D('PaymentApi')->where(array('id'=>$data['id']))->save(array('status'=>$data['status']));
    		if($ret > 0) {
    			$result['status'] = 'y';
    			$result['info'] = '请求提交成功！';
    		} else {
    			$result['status'] = 'n';
    			$result['info'] = '更新失败！';
    		}   
    	} catch(Exception $e) {
    		$result['status'] = 'n';
    		$result['info'] = $e->getMessage();
    	}
    	$this->ajaxReturn($result, 'JSON');
    }
    
    /*区域代理详情*/
    public function area_agent(){
        $user_id = I('get.id');

        $data = D('User')->where(array('user_id'=>$user_id))->find();
        if(!$data){
                $this->error('非法操作');
        }
        
        $upgrade_profit=M('UserAgentProfit')->where(array('user_id'=>$user_id))->select(); 
        $AreaAgent=M('AreaAgent')->select();       
        $nname=M('AreaAgent')->where(array('nid'=>$upgrade_profit['agent_nid']))->select('name');

        $name_data='';
        foreach ($AreaAgent as  $val){
           $name_data[$val['nid']]= $val['name'];   
        }

        $data['agent_name'] = D('UserAgentProfit')->get_name_by_nid($data['agent_nid']);
		
		$count = D('UserAreaAgent')->where(array('user_id'=>$user_id))->count();
	
		$agent_type = 'pcity';
		if(!$count){
			$agent_type = 'common';
		}else{
			//获取会员的代理
			$count2 = D('UserAreaAgent')->field('area_name')->where(array('user_id'=>$user_id,'agent_nid'=>'country'))->count();
			if($count2){
				$agent_type = 'country';
			}
		}

		$agent_types = array(
			'pcity'=>'省市代理',
			'common'=>'普通会员',
			'country'=>'全国代理',
		);
		
		//获取会员的代理-省级
		$user_province_list = D('UserAreaAgent')->field('area_name')->where(array('user_id'=>$user_id,'agent_nid'=>'province'))->select();
		
		//获取会员的代理-城市
		$user_city_list = D('UserAreaAgent')->field('area_name')->where(array('user_id'=>$user_id,'agent_nid'=>'city'))->select();
		foreach($user_city_list as $key=>$val){
			$info = D('City')->alias('c')->field('c.*,p.name province_name')->join('__PROVINCE__ AS p ON p.code=c.province_code','LEFT')->cache(true)->where(array('c.name'=>$val['area_name']))->find();
			$user_city_list[$key]['province_name'] = $info['province_name'];
		}

        $this->assign('upgrade_profit',$upgrade_profit);
        $this->assign('name_data',$name_data);
        $this->assign('agent_type',$agent_type);
        $this->assign('agent_types',$agent_types);
        $this->assign('user_province_list',$user_province_list);
        $this->assign('user_city_list',$user_city_list);
        $this->assign('data',$data);
        $this->display();
    }
    
    /*区域代理设置*/
    public function edit_agent(){
    	$this->check('3_0');
        if(IS_POST){
			$user_id = I('post.id');
			$agent_type = I('post.agent_type','common');
			
			//会员信息
			$data = D('User')->where(array('user_id'=>$user_id))->find();
			if(!$data){
				$this->error('非法操作');
			}
			$config_id = $data['config_id'];
			
			//代理类型错误
			$agent_type_arr = array('pcity','common','country');
			if(!in_array($agent_type,$agent_type_arr)){
				$this->error('代理类型错误');
			}

			if($agent_type=='pcity'){//省市代理
				$province = I('post.province');
				$city = I('post.city');
				foreach($province as $key=>$val){
					$temp = array('agent_nid'=>'province','area_name'=>$val);
					$area_data[] = $temp;
				}
				
				foreach($city as $key=>$val){
					$temp = array('agent_nid'=>'city','area_name'=>$val);
					$area_data[] = $temp;
				}

				$check_area = D('UserAgentProfit')->check_area($area_data);//检验代理地区
	
				if(!is_array($check_area)){
					$this->error('修改失败：'.$check_area);
				}
				
				D('UserAreaAgent')->where(array('user_id'=>$user_id))->delete();//删除
				foreach($check_area as $key=>$val){
					$val['user_id'] = $user_id;
					$val['config_id'] = $config_id;
					D('UserAreaAgent')->add($val);//新增
				}
			}elseif($agent_type=='country'){//全国代理
				$area_data[] = array('agent_nid'=>'country','全国');
				$check_area = D('UserAgentProfit')->check_area($area_data);//检验会员id
				if(!is_array($check_area)){
					$this->error('修改失败：'.$check_area);
				}
				
				D('UserAreaAgent')->where(array('user_id'=>$user_id))->delete();//删除
	
				$check_area['user_id'] = $user_id;
				$check_area['config_id'] = $config_id;
				D('UserAreaAgent')->add($check_area);//新增
				
			}else{//普通会员
				D('UserAreaAgent')->where(array('user_id'=>$user_id))->delete();//删除
			}


			$this->success('区域设置成功',U('User/index'));

        }else{
			$user_id = I('get.id');

			$data = D('User')->where(array('user_id'=>$user_id))->find();
			if(!$data){
					$this->error('非法操作');
			}
			
			//
			$user_agent_list = D('UserAreaAgent')->where(array('user_id'=>$user_id))->select();
			
			$max_id = count($user_agent_list)+1;
			$agent_type = 'pcity';
			if(empty($user_agent_list)){
				$agent_type = 'common';
			}elseif($user_agent_list[0]['agent_nid']=='country'){
				$agent_type = 'country';
			}

			$data['agent_name'] = D('UserAgentProfit')->get_name_by_nid($data['agent_nid']);
			$agent_list= D('UserAgentProfit')->get_agent_list(1,-1);
			
			//获取所有省份
			$all_province = D('Province')->cache(true)->select();
			
			//获取省份
			$user_province_list = D('UserAreaAgent')->field('area_name')->where(array('user_id'=>$user_id,'agent_nid'=>'province'))->select();
			$user_province_name = i_array_column($user_province_list, 'area_name');
	
			foreach($all_province as $key=>$val){
				$all_province[$key]['is_user'] = 0;
				if(in_array($val['name'],$user_province_name)){
					$all_province[$key]['is_user'] =1;
				}
			}
			
			$user_city_list = D('UserAreaAgent')->field('area_name')->where(array('user_id'=>$user_id,'agent_nid'=>'city'))->select();
			foreach($user_city_list as $key=>$val){
				$info = D('City')->alias('c')->field('c.*,p.name province_name')->join('__PROVINCE__ AS p ON p.code=c.province_code','LEFT')->cache(true)->where(array('c.name'=>$val['area_name']))->find();
				$user_city_list[$key]['city_code'] = $info['code'];
				$user_city_list[$key]['province_name'] = $info['province_name'];
				$user_city_list[$key]['province_code'] = $info['province_code'];
			}
			$this->assign('agent_list',$agent_list);
			$this->assign('user_agent_list',$user_agent_list);
			$this->assign('data',$data);
			$this->assign('agent_type',$agent_type);
			$this->assign('max_id',$max_id);
			$this->assign('all_province',$all_province);
			$this->assign('user_city_list',$user_city_list);
			$this->display();      
        }
    }
    
    /*
     * 图片审核
     */
    public function img_exa() {

    	$this->check('3_1');
    	if (IS_POST) {
    		$map['login_name'] = I("post.login_name");
    		$map['config_name'] = I("post.config_name");
    		$map['timestar'] = I("post.timestar");
    		$map['timeend'] = I("post.timeend");
    		$_GET['p'] = 1;
    	
    	}
    	if (!empty($map['timestar']) && !empty($map['timeend'])) {
    		$stime1 = strtotime($map['timestar'] . ' 00:00:00');
    		$etime1 = strtotime($map['timeend'] . ' 00:00:00');
    		$where['add_time'] = array('between', array($stime1, $etime1));
    	}
    	if (!empty($map['login_name'])) {
    		$where['login_name'] = $map['login_name'];
    	}
    	if (!empty($map['config_name'])) {
    		$where['config_name'] = $map['config_name'];
    	}
    	
    	$count =  M('MerImgOrder')->where($where)->count();
    	
    	$page = new \Think\Page ($count, 10, $map);
    	//显示分页
    	$show = $page->show();
       
        $img_order=M('MerImgOrder')
			        ->where($where)
			        ->limit($page->firstRow . ',' . $page->listRows)
			        ->select();
		
		$status= ['0'=>'待审核' , '1'=>'审核成功','2'=>'审核失败'];
        $this->assign('img',$img);
        $this->assign('img_order',$img_order);
        $this->assign('page',$show);
        $this->assign('status',$status);
        $this->display();  
    }
    
    /*
     * 图片审核
     */
    public function pass_exa() {
    	$this->check('3_1');
        $id=I("get.id");
        $status = M('MerImgOrder')->where(['user_id'=>$id])->getField('status');
        if($status==1){
            $this->success('该用户已审核！');
        }else{
            $data['status'] = 1;
            $user_data['img_confirm']=1;
            $user = M('MerImgOrder')->where(['user_id'=>$id])->save($data);
            $am = M('user')->where(['user_id'=>$id])->save($user_data);

            if($user){
                $this->success('审核成功！');
            } else {
                $this->error('审核失败！');
            }
        }
    }
    
    /*
     * 审核详情
     */
    public function img_details() {
    	$this->check('3_1');
        $id=I("get.id");
        
        $fileType=array(
        		array('type'=>'1010','name'=>'手持身份证'),
        		array('type'=>'1011','name'=>'身份证正面'),
        		array('type'=>'1012','name'=>'身份证反面'),
        		array('type'=>'1020','name'=>'银行卡正面'),
        		array('type'=>'1021','name'=>'银行卡反面 '),
        );
       
        $mer = M("mer_apply")->where(['user_id'=>$id])->find();
        $img = M("MerImg")->where(['user_id'=>$id])->select();
        
        foreach ($img as $val){
        	$img_ff[$val['fileType'].'']=$val;
        }
        
        $status = M("MerImgOrder")->where(['user_id'=>$id])->field("status")->find();
        $statusa= ['0'=>'待审核' , '1'=>'审核成功','2'=>'审核失败'];
        $this->assign('mer',$mer);
      
        $this->assign('img',$img_ff);
        $this->assign('id',$id);
        $this->assign('fileType',$fileType);
        $this->assign('status',$status);
        $this->assign('statusa',$statusa);
        $this->display();  
    }
    
    /*
     * 审核失败
     */
    public function no_pass_exa() {
    	$this->check('3_1');
        $id=I("get.id");
   
        $status = M('MerImgOrder')->where(['user_id'=>$id])->getField('status');
         if($status==2){
            $this->success('该用户已审核失败！');
        }else{
            $data['status'] = 2;
            $user_data['img_confirm']=2;
            $user = M('MerImgOrder')->where(['user_id'=>$id])->save($data);
            $am = M('user')->where(['user_id'=>$id])->save($user_data);
            if($user){
                $this->success('该用户审核失败！');
            } else {
                $this->error('审核出错！');
            }
        }
        
    }

    
    public function  jnpay(){
    	$this->check('3_1');
    
    	$id =I('post.id');
    	
    	$user= M('User')->where(['user_id'=>$id])->find();
    	if(empty($user)){
    		$res['status']='n';
    		$res['info']='会员不存在';
    		$this->ajaxReturn($res, 'JSON');
    	}
    	$mer=M('MerApply')->field('merId')->where(['user_id'=>$id])->find();
    	
    	if(empty($mer['merId'])){
    		$res=D('JnPay')->confirm($user);
    		
    		$this->ajaxReturn($res,'JSON');
    	}
    	else {
    	
    		
    		$res=D('JnPay')->query($mer['merId']);
    		
    		switch ($res['merchantStatus'])
    		{
    			case 'NORMAL':
    				//return $data;
    				break;
    			case 'CREATED':
    				//dump($res);
    				$res =D('JnPay')->confirm($user,'CREATED');
    				//return $data;
    				break;
    			case 'APPROVING':
    				//return $data;
    				break;
    			case 'CHANGEAPPROVING':
    			//	return $data;
    				break;
    			case 'CANCELLED':
    				$res =D('JnPay')->confirm($user,'CANCELLED');
    			//	return $data;
    				break;
    					
    		
    		}
    		$this->ajaxReturn($res, 'JSON');
    		
    		
    	}
    }
    
    public function	img_change(){
    	
    	$this->check('3_1');
    	if(IS_POST){
    		$data=I('post.');
    		$img_id = $data['img_id'];
    		$img= M('mer_img')->where(['img_id'=>$img_id])->find();
    		if(empty($img)){
    			
                
                    $this->error('图片信息不存在');
                    die();
    		}
    		$upload = new \Think\Upload();// 实例化上传类
    		$upload->maxSize   =     3145728 ;// 设置附件上传大小
    		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    		$upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
    		$upload->savePath  =     ''; // 设置附件上传（子）目录
    		// 上传文件
    		$info   =   $upload->upload();
    		if(!$info) {// 上传错误提示错误信息
    		
    			   $this->error('错误');
    			   die();
    		}else{// 上传成功
    			$file =$info['file'];
    			
    			$path = '/Public/upload/' . $file['savepath'] . $file['savename'];
    			$img_url ='http://'.$_SERVER['HTTP_HOST'].$path;
    			$re=M('mer_img')->where(['img_id'=>$img_id])->save(['img_url'=>$img_url]);
    			
    			if($re){
    				
    			 $this->success('修改成功！');
    			 die();
    			}else {
    				 $this->error('系统错误 稍后再试');
    				 die();
    			}
    			
    		}
    		 
    	}
    	
    	
    	$id =I('get.id');
    	
    	
    	$img= M('mer_img')->where(['img_id'=>$id])->find();
   
    	$this->assign($img);
    	$this->display();
    
    
    }
    
    public function  mercha_confirm(){
    	
    	$this->check('3_1');
    	if (IS_POST) {
    		$map['login_name'] = I("post.login_name");
    		$map['config_name'] = I("post.config_name");
    		$map['timestar'] = I("post.timestar");
    		$map['timeend'] = I("post.timeend");
    		$_GET['p'] = 1;
    		 
    	}
    	if (!empty($map['timestar']) && !empty($map['timeend'])) {
    		$stime1 = strtotime($map['timestar'] . ' 00:00:00');
    		$etime1 = strtotime($map['timeend'] . ' 00:00:00');
    		$where['add_time'] = array('between', array($stime1, $etime1));
    	}
    	if (!empty($map['login_name'])) {
    		$where['login_name'] = $map['login_name'];
    	}
    	if (!empty($map['config_name'])) {
    		$where['config_name'] = $map['config_name'];
    	}
    	 
    	$count =  M('MerApplyOrder')->where($where)->count();
    	 
    	$page = new \Think\Page ($count, 10, $map);
    	//显示分页
    	$show = $page->show();
    	 
    	$img_order=M('MerApplyOrder')
    	->where($where)
    	->limit($page->firstRow . ',' . $page->listRows)
    	->select();
    	
    	$status= ['0'=>'待审核' , '1'=>'审核成功','2'=>'审核失败'];
    	$this->assign('img',$img);
    	$this->assign('img_order',$img_order);
    	$this->assign('page',$show);
    	$this->assign('status',$status);
    	$this->display();
    	
    	 
    }
    
    public function  mercha_confirm_detail(){
    	$this->check('3_1');
    	$id=I("get.id");
    	$mer = M("mer_apply")->where(['user_id'=>$id])->find();
    	$fileType=array(
    			array('type'=>'1031','name'=>'店铺门头 '),
    			array('type'=>'1032','name'=>'店铺收银'),
    			array('type'=>'1033','name'=>'营业执照'),
    			array('type'=>'1034','name'=>'店铺内景 '),
    			
//     			'1031'=>'店铺门头 ',
//     			'1032'=>'店铺收银',
//     			'1033'=>'营业执照 ',
//     			'1034'=>'店铺内景',
//     			'1010'=>'身份证正面 ',
//     			'1011'=>'身份证反面',
//     			'1012'=>'银行卡正面',
//     			'1020'=>'银行卡反面',
//     			'1021'=>'手持身份证',
    	);
    	
  		$where['fileType']=array('in','1031,1032,1033,1034');
    	$where['user_id']=$id;
    	$img = M("MerImg")->where($where)->select();
    	
    	$status = M("MerApplyOrder")->where(['user_id'=>$id])->field("status")->find();
    	
    	foreach ($img as $val){
    		$img_ff[$val['fileType'].'']=$val;
    	}
    	
    	//	dump($img_ff);
    	$statusa= ['0'=>'待审核' , '1'=>'审核成功','2'=>'审核失败'];
    	$this->assign('mer',$mer);
    	$this->assign('img',$img_ff);
    	$this->assign('id',$id);
    	$this->assign('fileType',$fileType);
    	$this->assign('status',$status);
    	$this->assign('statusa',$statusa);
    	$this->display();
    	
    }
    
		
    public function	merchant_change(){
    	 
    	$this->check('3_1');
    	if(IS_POST){
    		$data=I('post.');
    		$id =I('post.id');
    	 	$fileType = I('post.type');
    	 	
    	 	$user= M('user')->where(['user'=>$id])->find();
    	 	if(empty($user)){
    	 		$this->error('没找到会员');
    	 		die();
    	 	}
    	 	
    	
    		$upload = new \Think\Upload();// 实例化上传类
    		$upload->maxSize   =     3145728 ;// 设置附件上传大小
    		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    		$upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
    		$upload->savePath  =     ''; // 设置附件上传（子）目录
    		// 上传文件
    		$info   =   $upload->upload();
    		if(!$info) {// 上传错误提示错误信息
    
    			$this->error('错误');
    			die();
    		}else{// 上传成功
    			$file =$info['file'];
    			$path = '/Public/upload/' . $file['savepath'] . $file['savename'];
    			$img_url ='http://'.$_SERVER['HTTP_HOST'].$path;
    			
    			$img= M('mer_img')->where(['user_id'=>$id,'fileType'=>$fileType])->find();
    				if(empty($img)){
    				$add['user_id']=$id;
    				$add['fileType']=$fileType;
    				$add['img_url']=$img_url;
    					$re=M('mer_img')->add($add);
    					
    				}else{
    					$re=M('mer_img')->where(['img_id'=>$img['img_id']])->save(['img_url'=>$img_url]);
    					
    				}
    				if($re){
    
    				$this->success('修改成功！');
    				die();
    			}else {
    				$this->error('系统错误 稍后再试');
    				die();
    			}
    			 
    		}
    		 
    	}
    	 
    	 
    	$id =I('get.id');
     	$fileType = I('get.type');
    	$user= M('user')->where(['user'=>$id])->find();
    	 if(empty($user)){
    	 		$this->error('没找到会员');
    	 		die();
    	 }
    	$img= M('mer_img')->where(['user_id'=>$id,'fileType'=>$fileType])->find();
    	
    	if(empty($img)){
    		$msg='没照片，可以添加';
    	}
    	
    	$this->assign($img);
    	$this->assign('id',$id);
    	$this->assign('msg',$msg);
    	$this->assign('type',$fileType);
    	$this->display();
    
    
    } 
    /*
     * 商户审核不通过
     */
    
    public function mechant_no_pass_exa() {
    	$this->check('3_1');
    	$id=I("get.id");
    	 
    	$status = M('MerApplyOrder')->where(['user_id'=>$id])->getField('status');
    	if($status==2){
    		$this->success('该用户已审核失败！');
    	}else{
    		$data['status'] = 2;
    		$user_data['merchant_confirm']=2;
    		$user = M('MerApplyOrder')->where(['user_id'=>$id])->save($data);
    		$am = M('user')->where(['user_id'=>$id])->save($user_data);
    		if($user){
    			$this->success('该用户审核失败！');
    		} else {
    			$this->error('审核出错！');
    		}
    	}
    
    }
    /*
     * 商户审核通过
     */
    public function mechant_pass_exa() {
    	$this->check('3_1');
    	$id=I("get.id");
    	$status = M('MerApplyOrder')->where(['user_id'=>$id])->getField('status');
    	if($status==1){
    		$this->success('该用户已审核！');
    	}else{
    		$data['status'] = 1;
    		$user_data['merchant_confirm']=1;
    		$user = M('MerApplyOrder')->where(['user_id'=>$id])->save($data);
    		$am = M('user')->where(['user_id'=>$id])->save($user_data);
    
    		if($user){
    			$this->success('审核成功！');
    		} else {
    			$this->error('审核失败！');
    		}
    	}
    }

        //提现记录列表页
    public function User_record()
    {
        $this->check('3_0');
        $id = I('get.id');

        $where['user_id']=$id;
        //查询出最后一条数据
        $count = M("BalanceRecord")->where($where)->count();
        $page = new \Think\Page ($count,10);
        //显示分页
        $show = $page->show();
        $data = M('BalanceRecord')->where(array('user_id'=>$id))->limit($page->firstRow . ',' . $page->listRows)->select();
        $array['type']=array('1'=>'对公','2'=>'对私');
        $array['status']=array('1'=>'审核中','2'=>'审核通过','3'=>'审核失败');
        $this->assign('array',$array);
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }
    //提现记录详情页面
    public function User_record_details()
    {
        $this->check('3_0');
        $id = I('get.id');
        $data = M('BalanceRecord')->where(array('id'=>$id))->find();
        $array['type']=array('1'=>'对公','2'=>'对私');
        $array['status']=array('1'=>'审核中','2'=>'审核通过','3'=>'审核失败');
        $this->assign('array',$array);
        $this->assign('data',$data);
        $this->display();
    }

    //执行审核
    public function authentication()
    {
        $id = I('get.id');
        $data['id'] = $id;
        $data['status'] = 2;
        $data['audited_time'] = time();
        $status = M('BalanceRecord')->where(array('id'=>$data['id']))->getField('status');
        if ($status == 2) {
            $this->success('该用户已审核！');
        } else {
            $r = M('BalanceRecord')->save($data);
            if ($r) {
                $this->success('审核成功！');
            } else {
                $this->error('审核失败！');
            }
        }

    }

    //拒绝审核
    public function jjsh()
    {
        $id = I('get.id');
        $data['id'] = $id;
        $data['status'] = 3;
        $data['audited_time'] = time();
        //修改提现状态为3  执行失败操作
        $r = M('BalanceRecord')->save($data);
        if($r){
            //查询出这个用户的提现信息
            $user = M('BalanceRecord')->where(array('id'=>$data['id']))->field('amount,user_id')->find();
            $user_money =M('User')->where(array('user_id'=>$user['user_id']))->getField('money');
            $add['user_id'] = $user['user_id'];
            $add['money'] = $user_money + $user['amount'];
            M('User')->save($add);
            $this->success('拒绝审核成功，金额已返还！');
        }else{
            $this->error('拒绝审核失败！');
        }

    }
    
}